package com.decoration.basic.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecCaseInfBase;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.findCondition.DecCaseFindCondition;
import com.decoration.basic.po.DecCaseInfVo;
import com.decoration.basic.service.DecCaseService;

@Controller
@RequestMapping("/admin")
public class CaseController extends BasicController {

	@Resource
	private DecCaseService decCaseservice;

	// 新增案例内容
	@RequestMapping(value = "/case/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult caseadd(JsonResult jsonResult, @RequestBody DecCaseInfBase decCaseInfBase) {
		if (null == decCaseInfBase || "".equals(decCaseInfBase)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("新增的案例信息为空,请重试!");
			return jsonResult;
		}

		int res = decCaseservice.add(decCaseInfBase);
		if (res > 0) {
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("新增案例信息成功");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.FAILED_CODE);
		jsonResult.setMsg("新增案例信息失败");
		return jsonResult;
	}

	// 修改案例内容
	@RequestMapping(value = "/case/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult casemodify(JsonResult jsonResult, @RequestBody DecCaseInfBase decCaseInfBase) {
		if (null == decCaseInfBase || "".equals(decCaseInfBase)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("修改的案例信息为空,请重试!");
			return jsonResult;
		}
		if (null == decCaseInfBase.getId() || "".equals(decCaseInfBase.getId())) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("修改的案例id为空,请重试!");
			return jsonResult;
		}

		int res = decCaseservice.update(decCaseInfBase);
		if (res > 0) {
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("修改案例列表成功");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.FAILED_CODE);
		jsonResult.setMsg("修改案例列表失败");
		return jsonResult;
	}

	//删除案例信息
	@RequestMapping(value = "/case/del", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delCarousel(JsonResult jsonResult, @RequestBody DecCaseInfBase decCaseInfBase) {
		if (null == decCaseInfBase.getId()) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要删除的案例ID为空,请重试!");
			return jsonResult;
		}

		int res = decCaseservice.delete(decCaseInfBase.getId());
		if (res > 0) {
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("删除成功");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.FAILED_CODE);
		jsonResult.setMsg("案例删除失败,请重试!");
		return jsonResult;
	}

	//获取案例列表
	@RequestMapping(value = "/case/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getCarouselList(JsonResult jsonResult, @RequestBody DecCaseFindCondition findCondition) {
		setPagerInfo(findCondition);
		List<DecCaseInfBase> list = decCaseservice.getCaseList(findCondition);
		Integer count = decCaseservice.getCaseListSize(findCondition);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(count);
		jsonResult.setData(list);
		return jsonResult;
	}

	//获取案例详情
	@RequestMapping(value = "/case/{caseId}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarousel(JsonResult jsonResult, @PathVariable Integer caseId) {
		DecCaseInfVo decCase = decCaseservice.findById(caseId);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setData(decCase);
		return jsonResult;
	}
}
